﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using MaxLeaf.DB;
using System.Data;


namespace MaxLeaf.DAL.Web
{
    /// <summary>
    /// 物料上级组
    /// </summary>
    public class ImInvGroupDAL : BaseDAL
    {
        private new enum GetDataType
        {
            OrgT_Item
            , OrgCat_Name

        }

        const string FIELDS = "Cat_Name";
        public ImInvGroupDAL()
            : base()
        {
            strTableName = "mary_category";
            strKeyFieldName = "FItemID";
            strFieldNames = FIELDS;
        }



        public override void Update(DBAccess dbOrgCon, DBAccess dbCon, string strKeyValue)
        {

            string strAddFieldNames = "FItemID";
            strFieldNames = FIELDS;



            string strFitemId = strKeyValue;
            DataTable dtData = dbCon.GetTableData(strTableName, new Dictionary<string, object> { { "FItemID", strFitemId } }, strAddFieldNames +","+ strFieldNames);
            DataRow drData = null;
            LogStatus logStatus = LogStatus.Update;

            DataRow drT_Item = DAL_GetData(dbOrgCon, dbCon, (int)GetDataType.OrgT_Item, strFitemId).Rows[0];




            GetCat_id(dbOrgCon, dbCon, CvtFunc.ToString(drT_Item["fnumber"]));



        }





        private string GetCat_id(DBAccess dbOrgCon, DBAccess dbCon, string strFnumber)
        {
            string[] arrFnumber = strFnumber.Split('.');
            string strCat_id = string.Empty;
            mary_categoryDAL catDal = new mary_categoryDAL();
            catDal.mstrItemFNumber = strFnumber;
            for (int i = 1; i < arrFnumber.Length; i++)
            {
                string strParentCat_id = "0";
                if (i != 1)
                {
                    strParentCat_id = strCat_id;
                }
                //得到每级的cat_id
                strCat_id = catDal.GetCat_id(dbOrgCon, dbCon, strParentCat_id, string.Join(".", arrFnumber, 0, i + 1));
            }




            return strCat_id;

        }

        //private string GetCat_Name(DBAccess dbOrgCon, DBAccess dbCon, string[] arrFnumber, int i)
        //{
        //    string strFnumber = string.Join(".", arrFnumber, 0, i + 1);

        //    return CvtFunc.ToString(DAL_GetData(dbOrgCon, dbCon, (int)GetDataType.OrgCat_Name, strFnumber).Rows[0]["Fname"]);
        //}




        protected override DataTable DAL_GetData(DBAccess dbOrgCon, DBAccess destCon, int intdataType, object objParam)
        {
            GetDataType dataType = (GetDataType)intdataType;

            if (dataType == GetDataType.OrgT_Item)
            {
                return GetT_Item(dbOrgCon, CvtFunc.ToString(objParam));
            }

            else if (dataType == GetDataType.OrgCat_Name)
            {
                return GetCat_Name(dbOrgCon, CvtFunc.ToString(objParam));
            }



            return base.DAL_GetData(dbOrgCon, destCon, intdataType, objParam);
        }





        private DataTable GetT_Item(DBAccess dbOrgCon, string strFitemID)
        {

            DataTable dtT_Item = null;
            if (dtT_Item == null)
            {
                dtT_Item = dbOrgCon.GetTableData("T_Item", new Dictionary<string, object> { { "FItemID", strFitemID } });

            }
            return dtT_Item;
        }



        private DataTable GetCat_Name(DBAccess dbOrgCon, string strFNumber)
        {
            return dbOrgCon.GetTableData("T_Item", new Dictionary<string, object> { { "fnumber", strFNumber }, { "FItemClassID", "4" } }, "FName,FItemID");

        }


        //public override void Delete(DBAccess dbOrgCon, DBAccess dbCon, string strKeyValue)
        //{
        //    throw new MaxLeafException("Delete mary_category 没实现");
        //}
    }
}